var roomArrayForSelect = null;
var selectedRoom = null;

$(function() {
	_addChangeListener("#room_select", function() {
		var option = $('#room_select option:selected');
		if(view && view.data) {view.data.roomName = option.text();}
		
		selectedRoom = getSelectedRoom();
		
		var roomId = $('#room_select').val();
		if(load_bed_select_by_roomId) {
			load_bed_select_by_roomId(roomId);
		}
		
		if(load_roomAsset_by_roomId) {
			load_roomAsset_by_roomId(roomId);
		}
	});
	
	_addChangeListener("#room_cond_select", function() {
		var roomId = $('#room_cond_select').val();
		if(load_bed_cond_select_by_roomId) {
			load_bed_cond_select_by_roomId(roomId);
		}
		
		if(load_bed_by_roomId) {
			load_bed_by_roomId(roomId);
		}
	});
});

load_room_select_by_floorId = function(floorId) {
	if($('#room_select').length==0) {return;}

	$('#room_select').html('<option value="" selected></option>');
	$('#bed_select').html('<option value="" selected></option>');
	roomArrayForSelect = null;
	
	var loginedUser = getLoginedUser();
	if(!floorId || floorId=='') {return;}

	find_basic_by_json(path+'/room/find', {floorId: floorId}, function(response) {
		roomArrayForSelect = response.data;
		
		for(var i=0; i<response.data.length; i++) {
			$('#room_select').append('<option value="' + response.data[i].roomId
				+ '">' + response.data[i].roomNumber + '房 ' + response.data[i].roomType + '</option>');
		}
	});
}

load_room_cond_select_by_floorId = function(floorId) {
	if($('#room_cond_select').length==0) {return;}

	$('#room_cond_select').html('<option value="" selected></option>');
	$('#bed_cond_select').html('<option value="" selected></option>');
	$('#vue_vm_list div').hide();
	
	var loginedUser = getLoginedUser();
	if(!floorId || floorId=='') {return;}

	find_basic_by_json(path+'/room/find', {floorId: floorId}, function(response) {
		for(var i=0; i<response.data.length; i++) {
			$('#room_cond_select').append('<option value="' + response.data[i].roomId
				+ '">' + response.data[i].roomNumber + '房 ' + response.data[i].roomType + '</option>');
		}
	});
}

var getSelectedRoom = function() {
	var roomId = $('#room_select').val();
	
	if(roomArrayForSelect) {
		for(var i=0; i<roomArrayForSelect.length; i++) {
			if(roomArrayForSelect[i].roomId == roomId) {
				return roomArrayForSelect[i];
			}
		}
	}
}